package com.ccrego.haiyi.quickysee;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;

import com.ccrego.haiyi.account.UrlCenter;
import com.ccrego.haiyi.app.bean.IsExistEntity;
import com.ccrego.haiyi.common.http.MyHttpRequest;
import com.ccrego.haiyi.db.DbConstant;
import com.ccrego.haiyi.db.PushMessageEntity;
import com.ccrego.haiyi.db.PushMessageEntityDao;
import com.pachong.android.baseuicomponent.LoadState;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;

/**
 * Description:
 * Created by Andy Hong on 10/16/2017.
 */

public class QuickSeeFragment extends SwipeListFragment {
    private PushMessageEntityDao pushMessageDao;
    private QuickSeeAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pushMessageDao = DbConstant.getPushMessageDaoSession(getContext().getApplicationContext()).getPushMessageEntityDao();
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        mAdapter = new QuickSeeAdapter(getContext());

        return mAdapter;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getRecyclerView().setBackgroundColor(Color.parseColor("#F4F4F4"));
        setPullToRefreshEnable(false);
        startLoading();
        mAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PushMessageEntity entity = mAdapter.getData().get(position);
                if (entity.getType() == 1) {
                    //计算阅读量
                    setReadNum(entity.getId());

                }
            }
        });
    }

    private void setReadNum(final Long id) {
        MyHttpRequest request = new MyHttpRequest(getContext());
        String requestUrl = UrlCenter.USER_REDD_NUM+id;
        request.put(requestUrl, null, new DataRequestListener<Object>(Object.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                isExistMessage(id);
                /**
                 * 判断该文章是否存在
                 */
//                isExistMessage(id);
//                Intent intent = new Intent(getActivity(), MessageDetailActivity.class);
//                intent.putExtra("id", id);
//                startActivity(intent);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(getContext(),strMsg);
            }
        });


    }

    private void isExistMessage(final Long id) {
        MyHttpRequest request= new MyHttpRequest(getContext());
        String id1 = id+"";
        String requestUrl = UrlCenter.USER_ARTICLE_EXIST+id1;
        request.get(requestUrl, null, new DataRequestListener<IsExistEntity>(IsExistEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(IsExistEntity o) {
                super.onSuccess(o);
                if(o.getSuccess()==true){
                Intent intent = new Intent(getActivity(), MessageDetailActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                }else{
                   EasyToast.showToast(getContext(),o.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
//                EasyToast.showToast(getContext(),strMsg);
            }
        });
    }

    @Override
    public void onStartLoading() {
        getAdapter().getData().addAll(pushMessageDao.queryBuilder().list());
        getAdapter().notifyDataSetChanged();
        changeLoadState(LoadState.FINISH);
        if (getAdapter().getData().size() != 0) {
            ((QuickSeeActivity) getActivity()).getCustomToolbar().addRightButton("清空", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialogFragment dialogFragment = new AlertDialogFragment();
                    dialogFragment.setTitle("提示")
                            .setMessage("确认清空消息吗?")
                            .setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialogFragment.dismiss();
                                }
                            })
                            .setPositiveButton("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    clear();
                                    dialogFragment.dismiss();
                                    ((QuickSeeActivity) getActivity()).getCustomToolbar().clearRight();
                                }
                            }).show(getFragmentManager());

                }
            });
        }

    }

    public void clear() {
        pushMessageDao.deleteAll();
        getAdapter().getData().clear();
        getAdapter().notifyDataSetChanged();
    }


}
